package client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.Serializable;

import javax.swing.*;


public class CreazioneRazza extends JFrame implements ActionListener,Serializable {
	
	private String razza = new String();
	private String nutriz = new String();
	
	public JLabel razzalabel;
	public JTextField razzafield;
		
	private ButtonGroup sceltanutriz; 
	private JRadioButton erbivoro;
	private JRadioButton carnivoro;
	private JButton ok;
	
	public CreazioneRazza(){
		super("Creazione razza");
		setExtendedState(MAXIMIZED_BOTH);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.CENTER;
		
		razzalabel = new JLabel("Nome razza");
		razzalabel.setForeground(Color.WHITE);
		
		JPanel pannello1 = new JPanel(new GridLayout(2,1));
				
		sceltanutriz = new ButtonGroup();
		erbivoro = new JRadioButton("Erbivoro", true);
		carnivoro = new JRadioButton("Carnivoro", false);
		
		pannello1.add(erbivoro);
		pannello1.add(carnivoro);
		
		sceltanutriz.add(erbivoro);
		sceltanutriz.add(carnivoro);
				
		ok = new JButton("OK");
		ok.setPreferredSize(new Dimension(60,35));
		ok.addActionListener(this);
		
		razzafield = new JTextField(15);
		razzafield.addActionListener(this);
		
		JPanel pannello = new JPanel();
		pannello.setLayout(new FlowLayout());
				
		pannello.add(razzalabel);
		pannello.add(razzafield);
		pannello.add(pannello1);
		pannello.add(ok);
		
		pannello.setOpaque(false);
		pannello1.setOpaque(false);
				
		PannelloSfondo pannello2 = new PannelloSfondo(new File("sfondo2.jpg"));
		pannello2.setLayout(new GridBagLayout());
		
		pannello2.add(pannello,c);
				
		add(pannello2, BorderLayout.CENTER);
		
		setVisible(true);
		
		synchronized(this){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void actionPerformed(ActionEvent e) {
		synchronized(this){
			razza = razzafield.getText();
			if((erbivoro.isSelected()&&carnivoro.isSelected())||((!erbivoro.isSelected())&&(!carnivoro.isSelected())))
				nutriz="errore";
			else if(erbivoro.isSelected())
				nutriz="Erbivoro";
			else
				nutriz="Carnivoro";
			
			notifyAll();
			dispose();
			}
	}
	
	public String getRazza(){
		return razza;
	}

	public String getNutriz(){
		return nutriz;
	}
}
